'use strict';

exports.main = async (event, context) => {
	try {
		const db = uniCloud.database();
		const collection = db.collection('data');
		
		// 获取最新的一条数据
		const result = await collection
			.orderBy('timestamp', 'desc')
			.limit(1)
			.get();
		
		if (result.data.length === 0) {
			return {
				code: -1,
				message: '暂无数据'
			};
		}
		
		const latestData = result.data[0];
		
		return {
			code: 0,
			message: '获取最新数据成功',
			data: latestData
		};
		
	} catch (error) {
		console.error('获取最新数据失败:', error);
		return {
			code: -1,
			message: '获取最新数据失败',
			error: error.message
		};
	}
};